package com.egd.imapp.contoller.activity.contact;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.model.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

public class CreateNewGroupActivity extends AppCompatActivity {
    private static final String TAG = "TAG";
    private EaseTitleBar mEaseTitleBar;
    private EditText et_new_group_name;
    private EditText et_new_group_desc;
    private CheckBox cb_new_group_public;
    private CheckBox cb_new_group_invite;
    private Button bt_new_group_create;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_new_group);
        
        initView();
        
        initListener();
    }

    private void initListener() {
        bt_new_group_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CreateNewGroupActivity.this, PickContactActivity.class);

                startActivityForResult(intent, 1);
            }
        });
    }

    private void initView() {
        mEaseTitleBar = findViewById(R.id.etb_create_new_group);
        et_new_group_name = findViewById(R.id.et_new_group_name);
        et_new_group_desc = findViewById(R.id.et_new_group_desc);
        cb_new_group_public = findViewById(R.id.cb_new_group_public);
        cb_new_group_invite = findViewById(R.id.cb_new_group_invite);
        bt_new_group_create = findViewById(R.id.bt_new_group_create);
        
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 成功获取到联系人
        if (resultCode == RESULT_OK){

            String[] members = data.getStringArrayExtra("members");
            Log.e(TAG, "onActivityResult: members = "+ members );

            if (members == null){
                members = new String[0];
            }

            // 创建群
            createGroup(members);
        }
    }

    private void createGroup(String[] members) {
        String groupName = et_new_group_name.getText().toString();
        String groupDesc = et_new_group_desc.getText().toString();

        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                // 去环信服务器创建群
                EMGroupOptions options = new EMGroupOptions();

                options.maxUsers = 100;
                EMGroupManager.EMGroupStyle groupStyle = null;

                if (cb_new_group_public.isChecked()){ // 群公开
                    if (cb_new_group_invite.isChecked()){// 开放群邀请
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
                    } else {
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePublicJoinNeedApproval;
                    }
                }else {
                    if (cb_new_group_invite.isChecked()){// 不开放群邀请
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateMemberCanInvite;
                    } else {
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateOnlyOwnerInvite;
                    }
                }

                options.inviteNeedConfirm = true;
                options.style = groupStyle; // 创建群的类型

                try {

                    // 参数一：群名称；参数二：群描述；参数三：群成员；参数四：原因；参数五：参数设置
                    EMGroup emGroup = EMClient.getInstance().groupManager().createGroup(groupName, groupDesc, members, "申请加入群", options);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(CreateNewGroupActivity.this, "创建群成功", Toast.LENGTH_SHORT).show();
                            Log.e(TAG, "run: 创建群成功 emGroup id  = " + emGroup.getGroupId() + " 人数 = "+ emGroup.getMemberCount() );
                            // 结束当前页面
                            finish();
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(CreateNewGroupActivity.this, "创建群失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
    }
}